package main

import (
	"fmt"
	"time"
)

func syncFun(id int, jobs <-chan int, results chan<- int) {
	for j := range jobs {
		fmt.Printf(" 任务:%d 开始， %v <- job \n", id, j)
		time.Sleep(1 * time.Second)
		results <- j * 2
		fmt.Printf(" 任务:%d 结束， results <- %v\n", id, j*2)

	}
}

func main() {
	jobs := make(chan int, 100)
	results := make(chan int, 100)

	// 开启3个goroutine
	for i := 0; i < 3; i++ {
		go syncFun(i, jobs, results)
	}

	// 发送5个任务
	for j := 0; j < 5; j++ {
		jobs <- j
	}
	close(jobs)

	// 输出结果
	// for v := range results {
	// 	fmt.Printf("v: %v\n", v)
	// }
	for a := 0; a < 5; a++ {
		ret := <-results
		fmt.Printf("ret: %v\n", ret)
	}

}
